layui.config({
    base: '/js/layui-expand/' //插件路径
}).extend({
    enhanceform: 'enhanceform'
});

layui.use(['table', 'element','layer','laytpl','form','enhanceform','laypage','upload'], function(){
    var $ = layui.jquery, layer = layui.layer, table = layui.table, form = layui.form,
        upload = layui.upload,enhanceForm = layui.enhanceform;

    var studenttable = table.render({
        elem: '#student_table_id'
        ,id: 'studenttable_id'
        ,url:'/student/list'
        ,cellMinWidth: 100
        ,page: {
            limit:10,
            limits:[10,20,30,40,50]
        }
        ,cols: [
            [
                {field:'id',  title: 'ID', sort: true,hide:true}
                ,{field:'name',  width:200,title: '姓名', sort: true}
                ,{field:'remark',  title: '学籍号'}
                ,{field:'lno',  width:200,title: '绑定标签号', sort: true}
                ,{field:'age',  title: '年龄'}
                ,{field:'sex',  title: '性别'}
                ,{field:'createtime',  title: '创建时间'}
                ,{field:'action', width:120, title: '操作',toolbar: '#bar',fixed: 'right'}
            ]
        ]
    });

    $('.layui-btn-add').on('click', function(){
        layer.open({
            type: 1,
            title: '添加学生信息',
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: false,
            btnAlign: 'c',
            btn: ['保存', '返回'],
            success: function(index,layero){
                form.render();
                // 解决按enter键重复弹窗问题
                $(':focus').blur();
                // 添加form标识
                index.addClass('layui-form');
                // 将保存按钮改变成提交按钮
                index.find('.layui-layer-btn0').attr({
                    'lay-filter' : 'studentsubmit',
                    'lay-submit' : ''
                });
            },
            yes: function(index,layero){
                form.on('submit(studentsubmit)', function(data) {
                    console.info(data);
                    $.ajax({
                        url:"/student/add",
                        type:'post',
                        dataType:'json',
                        data:JSON.stringify(data.field),
                        contentType: "application/json; charset=utf-8",
                        success:function(res){
                            if(res.code==200){
                                layer.alert('添加成功',{icon:1});
                                table.reload('studenttable_id', studenttable);
                                layer.close(index);
                            }else{
                                layer.alert(data.msg,{icon: 5});
                            }
                        },
                        error:function(){
                            layer.alert('操作失败！！！',{icon:5});
                        }
                    });
                });
            },
            content: $('#student_div_form').html()
        });
    });

    table.on('tool(student_table_filter)', function(obj){
        var data = obj.data ,layEvent = obj.event;
        if(layEvent === 'del'){
            layer.confirm('确认删除吗?', function(index){
                layer.close(index);
                $.ajax({
                    url:"/student/delete/"+data.id,
                    type:'get',
                    dataType:'json',
                    contentType: "application/json; charset=utf-8",
                    success:function(res){
                        if(res.code==200){
                            table.reload('studenttable_id', studenttable);
                            layer.alert('删除成功',{icon:1});
                        }else{
                            layer.alert(data.msg,{icon: 5});
                        }
                    },
                    error:function(){
                        layer.alert('操作失败！！！',{icon:5});
                    }
                });
            });
        } else if(layEvent === 'edit'){
            layer.open({
                type: 1,
                title: '修改学生信息',
                area: ['700px', '450px'],
                fixed: false, //不固定
                maxmin: false,
                btnAlign: 'c',
                btn: ['保存', '返回'],
                success: function (index, layero) {
                    // 解决按enter键重复弹窗问题
                    $(':focus').blur();
                    // 添加form标识
                    index.addClass('layui-form');
                    // 将保存按钮改变成提交按钮
                    index.find('.layui-layer-btn0').attr({
                        'lay-filter': 'studentsubmit',
                        'lay-submit': ''
                    });
                    form.render();
                    var eqform = new enhanceForm({
                        elem: '#student_form'
                    });
                    eqform.filling(data);

                },
                yes: function (index, layero) {
                    form.on('submit(studentsubmit)', function (data) {
                        $.ajax({
                            url: "/student/update",
                            type: 'post',
                            dataType: 'json',
                            data: JSON.stringify(data.field),
                            contentType: "application/json; charset=utf-8",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.alert('修改成功', {icon: 1});
                                    table.reload('studenttable_id', studenttable);
                                    layer.close(index);
                                } else {
                                    layer.alert(data.msg, {icon: 5});
                                }
                            },
                            error: function () {
                                layer.alert('操作失败！！！', {icon: 5});
                            }
                        });
                    });
                },
                content: $('#student_div_form').html()
            })
        }
    });

    var uploadListIns = upload.render({
        elem: '#importFile'
        ,url: '/student/uploadExcel'
        ,accept: 'file'
        ,exts: 'xlsx'
        ,auto: false
        ,before: function(){
            layer.load();
        }
        ,choose: function (obj) {
            var files = obj.pushFile();
            layer.confirm('确认导入吗?',{
                btn: ['确认','取消']
                ,yes:function(){
                    obj.preview(function(index, file){
                        obj.upload(index, file);
                    });
                }
                ,btn2:function () {
                    uploadListIns.config.elem.next()[0].value = ''
                }
            })
        }
        ,done: function(res){
            if(res.code == 200){
                layer.msg('上传成功');
            }else{
                layer.msg('上传失败');
            }
            studenttable.reload();
            layer.closeAll('loading');
        }
        ,error: function(index, upload){
            layer.msg('上传失败');
            layer.closeAll('loading');
        }
    });

});